package cn.com.xinjinfu.pdoulcProduct.activity.user;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.MyFriendsBean;
import cn.com.xinjinfu.pdoulcProduct.entity.MyFriendsBean.Invtors;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.xlistview.XListView;
import cn.com.xinjinfu.pdoulcProduct.xlistview.XListView.IXListViewListener;

/**
 * Created by android-adt on 2016/3/30.
 */
public class  MyFriendsListAct extends BaseActivity implements IXListViewListener{
    private User user;
    private XListView mListView;
    private  LinearLayout no_invest_view;
    private Handler mHandler;
    private RequestQueue requen;
    private MyAdapet mAdapter;
    private List<Invtors> friendslist;
    private int currpage = 1;
    private int numbers;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_friends);
        TitleManager.showTitle(this, "邀请好友列表", "");
        user = AccountManager.getInstance().getUser();
        no_invest_view = (LinearLayout) findViewById(R.id.no_invest_view);
        mListView = (XListView) findViewById(R.id.xListView);
        mListView.setPullLoadEnable(true);// 是否显示下拉刷新和上啦加载
        mListView.setPullRefreshEnable(false);
        mAdapter = new MyAdapet(this);
        mListView.setAdapter(mAdapter);
        mListView.setXListViewListener(this);
        mHandler = new Handler();
        requen = Volley.newRequestQueue(this);
        loginRequest(currpage);
    }

    /**
     * 网络请求
     */
    private void loginRequest(int page) {
        User user = AccountManager.getInstance().getUser();
        if (user != null) {
            final Map<String, String> parameters = DataHandler.getParameters("1420");
            parameters.put("id", user.getCurrUser().getSign());
            parameters.put("currPage", page+"");
            parameters.put("pageSize", "9");
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, null);
            requen.add(request);
        }
    }

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.d("好友列表 ==========" + jobj);
            if (JSONManager.getError(jobj) == -1){
                MyFriendsBean friendsBean = JSON.parseObject(jobj.toString(), MyFriendsBean.class);
                friendslist = friendsBean.getInvtors();

                if (currpage == 1){
                    numbers = friendsBean.getTotalNumbers();
                    if (friendslist == null || friendslist.isEmpty()){
                        no_invest_view.setVisibility(View.VISIBLE);
                    }else {
                        no_invest_view.setVisibility(View.GONE);
                        mAdapter.setMyList(friendslist);
                    }
                }else {
                    if (friendslist == null || friendslist.isEmpty()){
                        ToastManager.showShort(MyFriendsListAct.this, "没有更多数据");}
                    else {
                        mAdapter.addList(friendslist);
                    }
                }
            }
        }
    };
    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("刚刚");
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loginRequest(1);
                onLoad();
            }
        }, 1000);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loginRequest(++currpage);
                onLoad();
            }
        }, 1000);
    }

    private class MyAdapet extends BaseAdapter {
        private LayoutInflater mInflater;
        private DateFormat sdf;
        private List<Invtors> myList;


        public void setMyList(List list){
            myList.clear();
            myList.addAll(list);
            notifyDataSetChanged();
        }

        public void addList(List list) {
            myList.addAll(list);
            notifyDataSetChanged();
        }

        public MyAdapet(Context context) {
            this.mInflater = LayoutInflater.from(context);
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            myList = new ArrayList<>();
        }

        @Override
        public int getCount() {
            return myList == null ? 0 : myList.size();
        }

        @Override
        public Object getItem(int position) {
            return myList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_friends_list, null);
                holder = new ViewHolder();
                holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
                holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                holder.phone_num = (TextView) convertView.findViewById(R.id.phone_num);
                holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                convertView.setTag(holder);//绑定ViewHolder对象
            } else {
                holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
            }
            Invtors friend = myList.get(position);
            if (friend != null){
                holder.tv_id.setText((numbers-position) + "");
                if ((numbers-position) == 1){
                    mListView.setPullLoadEnable(false);
                }
                holder.tv_name.setText(friend.getRealityName());
                holder.phone_num.setText(friend.getMobile());
                holder.tv_time.setText(sdf.format(friend.getInvtDate()));
            }
            return convertView;
        }
    }
    public final class ViewHolder {
        public TextView tv_id;
        public TextView tv_name;
        public TextView phone_num;
        public TextView tv_time;
    }
}
